﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using LowLevelGraphics.Filter;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Experimental3 : Experimental1
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental3"/> class.
        /// </summary>
        public Experimental3()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental3"/> class.
        /// </summary>
        /// <param name="_experimental3">The _experimental3.</param>
        internal Experimental3(Experimental3 _experimental3)
        {

        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap _bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            new Experimental1().Execute(_bitmapCloned);

            UnsafeBitmap bitmapCloned = _bitmapCloned;
            UnsafeBitmap bitmap = _bitmap;

            Color colorCloned = Color.Transparent;
            Color color = Color.Transparent;

            List<Range> aRange = new List<Range>();

            bool bFound = false;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            for (int y = 0; y < nHeight; y++)
            {
                int nStartX = -1;
                int nEndX = -1;

                List<Color> aColor = new List<Color>();
                for (int x = 0; x < nWidth; x++)
                {
                    colorCloned = bitmap.GetPixel(x, y);
                    if (ColorFunctions.RGBChannelSum(colorCloned)>10)
                    {
                        while (ColorFunctions.RGBChannelSum(colorCloned)>10)
                        {
                            colorCloned = bitmapCloned.GetPixel(x, y);
                            if (x > nWidth - 1)
                            {
                                break;
                            }
                            x++;
                        }
                        nStartX = x;
                        while (!(ColorFunctions.RGBChannelSum(colorCloned)>10))
                        {
                            colorCloned = bitmapCloned.GetPixel(x,y);
                            color = bitmap.GetPixel(x, y);
                            if (x > nWidth - 1)
                            {
                                break;
                            }
                            aColor.Add(color);
                            x++;
                        }
                        while (ColorFunctions.RGBChannelSum(colorCloned)>10)
                        {
                            colorCloned = bitmapCloned.GetPixel(x, y);
                            color = bitmap.GetPixel(x, y);
                            if (x > nWidth - 1)
                            {
                                break;
                            }
                            x++;
                        }
                        nEndX = x;
                        aRange.Add(new Range(nStartX, nEndX, y, ColorFunctions.AverageColor(aColor)));
                        nStartX = -1;
                        nEndX = -1;
                        aColor.Clear();
                    }
                }
            }
            bitmap.Dispose();
            bitmapCloned.Dispose();
            Graphics g = Graphics.FromImage(_bitmap.InternalBitmap);
            foreach (Range range in aRange)
            {
                g.DrawLine(new Pen(new SolidBrush(range.Color), 1), range.StartX, range.Y, range.EndX,range.Y);
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Experimental3(this);
        }
    }
}
